package com.itheima.wechat;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itheima.MemberService;
import com.itheima.SetmealService;
import com.itheima.contant.SetmealRedisConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class WXSetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @Reference
    private MemberService memberService;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(HttpServletRequest request,
                             @com.itheima.annotion.Member Member member){

        if (member == null ){
            return Result.error("");
        }

        //从redis中获取套餐信息数据
        Jedis resource = jedisPool.getResource();
        String setmeal = resource.get(SetmealRedisConstant.UPDATE_SETMEAL_LIST);
        //判断在redis中是否查询到数据
        List<Setmeal> setmealList = null ;
        if (null == setmeal) {
            //如果redis中没有数据 那么就从数据库查询数据
            setmealList = setmealService.queryAll();
            try{
                //将查询到的套餐信息 以json格式存入redis
                String jsonSetmeal = JSON.toJSONString(setmealList);
                resource.set(SetmealRedisConstant.UPDATE_SETMEAL_LIST,jsonSetmeal);
                setmealList = JSON.parseObject(jsonSetmeal,new TypeReference<ArrayList<Setmeal>>(){});
                return Result.success("",setmealList);
            } catch (Exception e) {
                return Result.error("");
            }
        }
        List<Setmeal> setmealList1 = JSON.parseObject(setmeal,new TypeReference<ArrayList<Setmeal>>(){});
        return new Result(true,"查询成功",setmealList1);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findByIdBatch(id);
            //查询用户id,以及用户的家庭联系人等信息
            return Result.success("",setmeal);
        } catch (Exception e) {
            return Result.error("");
        }
    }

    @RequestMapping("/findFamily")
    public Result findFamily(HttpServletRequest request,
                             @com.itheima.annotion.Member Member member){

        if (member == null ){
            return Result.error("");
        }

        try {
            //查询用户的家庭联系人等信息
            List<Member> members =  memberService.queryMemberFamily(member.getId());
            return Result.success("",members);
        } catch (Exception e) {
            return Result.error("");
        }
    }

}
